<?php

require MX_SHOP_PATH . 'core/web/mroles/page_merch.php';
class Index_MxShopPage extends MrolesWebPage {

    public function main(){
        global $_W;

        include $this->template('mroles');
    }

    //首页数据
    public function iframe(){
        global $_W;
        $uid = $_W['manageid'];
        $goodsid = array(0);

        //验证令牌
        if(empty($_GPC['token']) || !checkToken($_GPC['token'])){
            show_json(0, '参数有误');
        }
        if($_W['isowner']){
            $goodsid = $this->getOwnerGoods();
        }
        //店主设备id
        if($_W['ismanage']){
            $mdids = $this->getMendianGoods();
            $goodsid = array_merge($goodsid, $mdids);
            unset($mdids);
        }
        //服务商设备id
        if($_W['ischannel']){
            $chids = $this->getMendianGoods(2);
            $goodsid = array_merge($goodsid, $chids);
            unset($chids);
        }
        if($_W['isoperator']){
            $opids = $this->getOperatorGoods();
            $goodsid = array_merge($goodsid, $opids);
            unset($opids);
        }
        $goodsid = implode(',', array_unique($goodsid));
        $goodsid = empty($goodsid) ? 0 : $goodsid;

        $ordernum = pdo_fetchcolumn('SELECT ifnull(count(*), 0) FROM '.tablename('mx_shop_service_order').' WHERE uniacid='.$_W['uniacid'].' AND goods_id in ('.$goodsid.') AND status=3');
        $orderprice = pdo_fetchcolumn('SELECT ifnull(sum(realprice), 0) FROM '.tablename('mx_shop_service_order').' WHERE uniacid='.$_W['uniacid'].' AND goods_id in ('.$goodsid.') AND status=3');

        $commission = pdo_fetch('SELECT * FROM '.tablename('mx_shop_service_commission').' WHERE uniacid='.$_W['uniacid'].' AND uid=:uid LIMIT 1', array(':uid'=>$uid));
        if($_W['ismanage']){
            $mdids = $this->getMendianGoods(1,1);
            $mdcomm = pdo_fetchall('SELECT * FROM '.tablename('mx_shop_service_commission').' WHERE uniacid='.$_W['uniacid'].' AND mendianid in ('.implode(',', $mdids).')');
            if(!empty($mdcomm)){
                foreach ($mdcomm as $item) {
                    $commission['total'] += $item['total'];
                    $commission['remain'] += $item['remain'];
                }
            }

        }

        $ordernumsort = pdo_fetchall('SELECT ifnull(count(o.id), 0) as num, g.name as goods, m.mendianname as mendian FROM '.tablename('mx_shop_service_order').' o LEFT JOIN '.tablename('mx_shop_service_goods').' g ON g.id=o.goods_id LEFT JOIN '.tablename('mx_shop_mendian').' m ON m.id=g.mendian_id WHERE o.uniacid='.$_W['uniacid'].' AND o.goods_id in ('.$goodsid.') AND o.status=3 GROUP BY m.id ORDER BY num DESC LIMIT 5');
        $orderpricesort = pdo_fetchall('SELECT ifnull(sum(o.realprice), 0) as price, g.name as goods, m.mendianname as mendian FROM '.tablename('mx_shop_service_order').' o LEFT JOIN '.tablename('mx_shop_service_goods').' g ON g.id=o.goods_id LEFT JOIN '.tablename('mx_shop_mendian').' m ON m.id=g.mendian_id WHERE o.uniacid='.$_W['uniacid'].' AND o.goods_id in ('.$goodsid.') AND o.status=3 GROUP BY m.id ORDER BY price DESC LIMIT 5');

        $maxnum = $maxprice = 0;
        foreach ($ordernumsort as $item) {
            $maxnum = $item['num'] > $maxnum ? $item['num'] : $maxnum;
        }
        foreach ($orderpricesort as $item) {
            $maxprice = $item['price'] > $maxprice ? $item['price'] : $maxprice;
        }
        include $this->template('mroles/iframe');
    }

    public function detail(){

        global $_W;
        $uid = $_W['manageid'];

//        $owner = pdo_fetchall('SELECT * FROM '.tablename('mx_shop_service_goods_owner').' WHERE uid=:uid AND deleted=0', array(':uid'=>$uid));
//
//        $goodsid = array(0);
//        foreach ($owner as $item){
//            $goodsid[] = $item['goods_id'];
//        }

        $goodsid = array(0);
        if($_W['isowner']){
            $goodsid = $this->getOwnerGoods();
        }

        //店主设备id
        if($_W['ismanage']){
            $mdids = $this->getMendianGoods();
            $goodsid = array_merge($goodsid, $mdids);
            unset($mdids);
        }

        //服务商设备id
        if($_W['ischannel']){
            $chids = $this->getMendianGoods(2);
            $goodsid = array_merge($goodsid, $chids);
            unset($chids);
        }

        if($_W['isoperator']){
            $opids = $this->getOperatorGoods();
            $goodsid = array_merge($goodsid, $opids);
            unset($opids);
        }

        $goodsid = implode(',', array_unique($goodsid));
        $goodsid = empty($goodsid) ? 0 : $goodsid;

        $ordernum = pdo_fetchcolumn('SELECT ifnull(count(*), 0) FROM '.tablename('mx_shop_service_order').' WHERE uniacid='.$_W['uniacid'].' AND goods_id in ('.$goodsid.') AND status=3');
        $orderprice = pdo_fetchcolumn('SELECT ifnull(sum(realprice), 0) FROM '.tablename('mx_shop_service_order').' WHERE uniacid='.$_W['uniacid'].' AND goods_id in ('.$goodsid.') AND status=3');

        $commission = pdo_fetch('SELECT * FROM '.tablename('mx_shop_service_commission').' WHERE uniacid='.$_W['uniacid'].' AND uid=:uid LIMIT 1', array(':uid'=>$uid));
        if($_W['ismanage']){
            $mdids = $this->getMendianGoods(1,1);
            $mdcomm = pdo_fetchall('SELECT * FROM '.tablename('mx_shop_service_commission').' WHERE uniacid='.$_W['uniacid'].' AND mendianid in ('.implode(',', $mdids).')');
            if(!empty($mdcomm)){
                foreach ($mdcomm as $item) {
                    $commission['total'] += $item['total'];
                    $commission['remain'] += $item['remain'];
                }
            }

        }

        $ordernumsort = pdo_fetchall('SELECT ifnull(count(o.id), 0) as num, g.name as goods, m.mendianname as mendian FROM '.tablename('mx_shop_service_order').' o LEFT JOIN '.tablename('mx_shop_service_goods').' g ON g.id=o.goods_id LEFT JOIN '.tablename('mx_shop_mendian').' m ON m.id=g.mendian_id WHERE o.uniacid='.$_W['uniacid'].' AND o.goods_id in ('.$goodsid.') AND o.status=3 GROUP BY m.id ORDER BY num DESC LIMIT 5');
        $orderpricesort = pdo_fetchall('SELECT ifnull(sum(o.realprice), 0) as price, g.name as goods, m.mendianname as mendian FROM '.tablename('mx_shop_service_order').' o LEFT JOIN '.tablename('mx_shop_service_goods').' g ON g.id=o.goods_id LEFT JOIN '.tablename('mx_shop_mendian').' m ON m.id=g.mendian_id WHERE o.uniacid='.$_W['uniacid'].' AND o.goods_id in ('.$goodsid.') AND o.status=3 GROUP BY m.id ORDER BY price DESC LIMIT 5');

        $maxnum = $maxprice = 0;
        foreach ($ordernumsort as $item) {
            $maxnum = $item['num'] > $maxnum ? $item['num'] : $maxnum;
        }
        foreach ($orderpricesort as $item) {
            $maxprice = $item['price'] > $maxprice ? $item['price'] : $maxprice;
        }

        include $this->template('mroles/index/detail');
    }

    public function quit()
    {
        global $_W, $_GPC;
        isetcookie('__mroles_'.$_W['uniacid'].'_session', -7*  86400);
//        isetcookie('__uniacid', -7*  86400);
//        unset($_SESSION['__mroles_uniacid']);
        header('location: ' . mrolesUrl('login', array('i'=>$_W['uniacid'])));
        exit;
    }

    public function updatepassword()
    {
        global $_W, $_GPC;
        if ($_W['ispost'])
        {
            $account = pdo_fetch('SELECT * FROM '.tablename('mx_shop_user').' WHERE id=:id AND isblack=0 LIMIT 1', array(':id'=>$_W['manageid']));

            $oldpsd = trim($_GPC['oldpasswd']);
            $newpsd = trim($_GPC['newpasswd']);
            $confirmpsd = trim($_GPC['confirmpasswd']);
            if(strlen($oldpsd) < 6 || strlen($oldpsd) > 20){
                show_json(0, '请输入 6 ~ 20 位的原账号密码');
            }

            if(strlen($newpsd) < 6 || strlen($newpsd) > 20){
                show_json(0, '请输入 6 ~ 20 位的新账号密码');
            }

            if($confirmpsd != $newpsd){
                show_json(0, '确认密码与新密码不一致');
            }

            if(md5($oldpsd.$account['salt']) != $account['pwd']){
                show_json(0, '原密码不正确！');
            }

            $salt=random(8);

            $user = array();
            $user['pwd'] = md5($newpsd . $salt);
            $user['salt'] = $salt;
            //$user['updatetime'] = time();

            pdo_update('mx_shop_user', $user, array('id'=>$account['id']));

            $url = mrolesUrl('login', array('i'=>$_W['uniacid']));
            show_json(1, array('url'=>$url));
        }

        include $this->template('mroles/index/updatepassword');
    }

    public function updatedetail(){

        include $this->template('mroles/index/updatedetail');
    }

}